package com.example.administrator.szb.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;

/**
 * Created by stone on 2017/12/29.
 */

public class SpannableStringUtil {
    private Context context;
    private  SpannableStringBuilder sb;
    public SpannableStringUtil(TextView view){
        context = view.getContext();
        sb = new SpannableStringBuilder("");
        view.setMovementMethod(LinkMovementMethod.getInstance());//接收点击事件
        view.setHighlightColor(Color.TRANSPARENT);//选中时无色
    }
    public  void clear(){
        sb.clear();
    }

    /**
     * 获取最后SpannableStringBuilder对象
     * @return
     */
    public  SpannableStringBuilder getText(){
        return sb;
    }

    /**
     * 追加文字
     * @param string
     */
    public  void addText(String string){
        sb.append(string);
    }

    /**
     * 追加文字（有颜色）
     * @param string
     * @param color
     * @return
     */
    public  SpannableString addTextWithColor(String string,int color){
        SpannableString spannableString = new SpannableString(string);
        addColor(string,spannableString,color);
        sb.append(spannableString);
        return spannableString;
    }

    /**
     * 追加文字（有点击事件）
     * @param string
     * @param listener
     * @return
     */
    public  SpannableString addTextWithClick(String string,ClickListener listener){
        SpannableString spannableString = new SpannableString(string);

        addClick(string,spannableString,listener);
        sb.append(spannableString);
        return spannableString;
    }
    public SpannableString addTextWithImg(String imgStr,int img ,ClickListener listener,int arr[]){
//带图片的文本
        SpannableString spannableStringImg=new SpannableString(imgStr);
        Drawable drawable=context.getResources().getDrawable(img);
        drawable.setBounds(0,0,arr[0],arr[1]);
        ImageSpan imageSpan=new ImageSpan(drawable);
        spannableStringImg.setSpan(imageSpan,0, imgStr.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        sb.append(spannableStringImg);
        return spannableStringImg;
    }
    /**
     * 追加文字（有点击事件和文字）
     * @param string
     * @param color
     * @param listener
     * @return
     */
    public  SpannableString addTextAll(String string,int color,ClickListener listener){
        SpannableString spannableString = new SpannableString(string);
        addColor(string,spannableString,color);
        addClick(string,spannableString,listener);
        sb.append(spannableString);
        return spannableString;
    }
    private  void addColor(String str,SpannableString spannableString,int color){
        spannableString.setSpan(new ForegroundColorSpan(color), 0, str.length(), Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);

    }
    private  void addClick(String str, SpannableString spannableString, final ClickListener listener){
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(View widget) {
                if (listener!=null){
                    listener.onClick();
                }
            }
        }, 0, str.length(), Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    public interface ClickListener{
        void onClick();
    }
}
